import { chatShareUpdate, chatShareList, chatShareNew, shareInfo } from '@/network/api'

const share = {
  state: {
    saveShareList: [],
    checkoutAll: false,
    conversation_id: -1,
    uuidName: {
      uuid: '',
      name: '',
    },
    messageInfo: {},
    shareHistory: {
      name: '',
      id: '',
    },
  },
  mutations: {
    SETHISTORY(state, params) {
      state.shareHistory = params
    },
    SETSHARENAMEUUID(state, uuid) {
      state.uuidName.uuid = uuid
    },
    SETSHARENAMENAME(state, name) {
      state.uuidName.name = name
    },
    SETSHARELIST(state, params) {
      state.saveShareList = params
    },
    SHARECLEAR(state) {
      state.saveShareList = []
    },
    SETALL(state, params) {
      state.checkoutAll = params
    },
    SETSHAREID(state, params) {
      console.log('params---------', params)
      state.conversation_id = params
    },
    SETSHAREMESSAGE(state, params) {
      state.messageInfo = params
    },
  },
  actions: {
    async IchatShareUpdate({ commit }, params) {
      try {
        const res = await chatShareUpdate(params)
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async IchatShareList({ commit }, params) {
      try {
        const res = await chatShareList(params)
        return res
      } catch (error) {
        console.log(error)
      }
    },
    async IchatShareNew({ commit, state }, params) {
      /**
       * 这里是选中的聊天列表 shareList
       * 这里是全选的状态 checkoutAll
       * 每次对话的id有了 conversationId
       * 这里要看checkoutAll的状态, 如果为true, 那么传conversationId, 否则传shareList
       *
       */

      let sendParams = {}
      if (state.shareHistory.id) {
        sendParams.conversation_id = state.shareHistory.id
      } else {
        if (state.checkoutAll) {
          sendParams.conversation_id = state.conversation_id
        } else {
          if (!state.saveShareList.length) return
          sendParams.message_ids = state.saveShareList
        }
      }

      try {
        // 需要获取chat里面的state
        const res = await chatShareNew(sendParams)
        commit('SETSHARE', false)

        commit('SETSHARENAMEUUID', res.share_uuid)
        commit('SETHISTORY', { name: '', id: '' })
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async IshareInfo({ commit }, params) {
      try {
        // 聊天详情
        const res = await shareInfo(params)
        //  let tempres =   JSON.parse(JSON.stringify(res))
        //     console.log(res)
        res.info.messages.shift()
        commit('SETSHAREMESSAGE', res.info)
      } catch (error) {
        return Promise.reject(error)
        console.log(error)
      }
    },
  },
}
export default share
